package com.canbot.u05.activity.agenda

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.adapter.AgendaTypeAdapter
import com.canbot.u05.activity.agenda.bean.AgendaDanceBean
import com.canbot.u05.activity.agenda.bean.AgendaTypeBean
import com.canbot.u05.activity.agenda.interfaces.IAgendaTypeLisenter
import com.canbot.u05.activity.presidemode.newcode.BaseHostActivity
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse
import com.canbot.u05.bean.AppInfo
import com.canbot.u05.utils.PathConst
import com.canbot.u05.utils.ToastTools
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_agenda_home_list.*

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class AgendaTypeActivity : BaseHostActivity() {
    companion object {
        val mAgendaTypeBeanList = ArrayList<AgendaTypeBean>()
    }

    private var mAgendaTypeBean: AgendaTypeBean = AgendaTypeBean()
    private lateinit var adapter: AgendaTypeAdapter
    private val voices = arrayOf(arrayOf("${PathConst.AGENDA_VOICE_DIR}gzlx_gzrc_u05_001.mp3", "12"),
            arrayOf("${PathConst.AGENDA_VOICE_DIR}gzlx_gzrc_u05_002.mp3", "30"))

    private var mSelectPos = -1

    override fun initEventData() {
        initIntent()
        getDatas()
        adapter = AgendaTypeAdapter(mAgendaTypeBeanList, this, object : IAgendaTypeLisenter {
            override fun onSelect(pos: Int) {
                mSelectPos = pos
                handlerTypeSelect(pos)
            }
        })
        recycleView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        recycleView.adapter = adapter
        playRandomVoiceWithEmotion(voices)
    }

    private fun getDatas() {
        if (mAgendaTypeBeanList.isEmpty()) {
            val agendaTypeBeanList = ArrayList<AgendaTypeBean>()
            val stringArray = resources.getStringArray(R.array.agenda_module_names)
            for (s in stringArray) {
                val element = AgendaTypeBean()
                element.type = s
                element.isSelect = mAgendaTypeBean.type == element.type
                agendaTypeBeanList.add(element)
            }
            val loadingDialog = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.load_hint))
            NetLoadUtil.getAppInfos(this, object : IResponse<String> {
                override fun onSucusse(result: String?) {
                    LoadingDialogUtils.closeDialog(loadingDialog)
                    result?.let {
                        val parseArray = JSON.parseArray(result, AppInfo::class.java)
                        val list: List<String> = parseArray.filter {
                            if (it.isMainFunction) {
                                true
                            } else {
                                it.appName == getString(R.string.module_name_ybjd) ||
                                        it.appName == getString(R.string.module_name_cxlk) ||
                                        it.appName == getString(R.string.module_name_wdby) ||
                                        it.appName == getString(R.string.module_name_hbxc)
                            }
                        }.map {
                            it.appName
                        }
                        mAgendaTypeBeanList.addAll(getIntersection(agendaTypeBeanList, list))
                        runOnUiThread {
                            adapter.notifyDataSetChanged()
                        }
                    }
                }

                override fun onFailuer(result: String?) {
                    LoadingDialogUtils.closeDialog(loadingDialog)
                    ToastTools.newInstance(this@AgendaTypeActivity).showText(result)
                }
            })
        }
    }

    fun getIntersection(listStatic: ArrayList<AgendaTypeBean>,
                        listDynamic: List<String>): ArrayList<AgendaTypeBean> {
        val result = ArrayList<AgendaTypeBean>()
        listStatic.forEach {
            if (it.type == getString(R.string.shutdown)) {
                result.add(it)

            } else if (listDynamic.contains(it.type)) {
                result.add(it)
            }
        }
        return result
    }

    private fun initIntent() {
        mAgendaTypeBean = intent.getSerializableExtra(DATA) as AgendaTypeBean
    }

    private val TAG = AgendaTypeActivity::class.java.simpleName
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val dateList: ArrayList<AgendaDanceBean> = data?.getSerializableExtra(DATA) as ArrayList<AgendaDanceBean>
        if (dateList.size > 0) {
            mAgendaTypeBean.data.clear()
            mAgendaTypeBean.data.addAll(dateList)
            updateState()
            when (requestCode) {
                REQUEST_DANCE -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_wdby)
                }
                REQUEST_MUSIC -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_ypbf)
                }
                REQUEST_CXLK -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_cxlk)
                }
                REQUEST_VIDEO -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_spbf)
                }
                REQUEST_YBJD -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_ybjd)
                }
                REQUEST_HBXC -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_hbxc)
                }
                REQUEST_GBYD -> {
                    mAgendaTypeBean.type = getString(R.string.module_name_gbyd)
                }
            }
            exit()
        } else {

        }
    }

    private fun updateState() {
        if (mSelectPos != -1) {
            mAgendaTypeBeanList.forEach {
                it.isSelect = false
            }
            mAgendaTypeBeanList[mSelectPos].isSelect = true
            adapter.notifyDataSetChanged()
        }
    }

    private fun handlerTypeSelect(pos: Int) {
        val agendaTypeBean = mAgendaTypeBeanList[pos]
        when (agendaTypeBean.type) {
            getString(R.string.module_name_spbf) -> {
                gotoDataActivity(REQUEST_VIDEO)
            }
            getString(R.string.module_name_wdby) -> {
                gotoDataActivity(REQUEST_DANCE)
            }
            getString(R.string.module_name_ypbf) -> {
                gotoDataActivity(REQUEST_MUSIC)
            }
            getString(R.string.module_name_cxlk) -> {
                gotoDataActivity(REQUEST_CXLK)
            }
            getString(R.string.module_name_ybjd) -> {
                /*mAgendaTypeBean.type = getString(R.string.module_name_ybjd)
                mAgendaTypeBean.data.clear()
                exit()*/
                gotoDataActivity(REQUEST_YBJD)
            }
            getString(R.string.module_name_gbyd) -> {
                /* mAgendaTypeBean.type = getString(R.string.module_name_gbyd)
                 mAgendaTypeBean.data.clear()
                 exit()*/
                gotoDataActivity(REQUEST_GBYD)
            }
            getString(R.string.module_name_hbxc) -> {
                /*   mAgendaTypeBean.type = getString(R.string.module_name_hbxc)
                   mAgendaTypeBean.data.clear()
                   exit()*/
                gotoDataActivity(REQUEST_HBXC)
            }
            getString(R.string.module_name_kqdk) -> {
                mAgendaTypeBean.type = getString(R.string.module_name_kqdk)
                mAgendaTypeBean.data.clear()
                exit()
            }
            getString(R.string.shutdown) -> {
                mAgendaTypeBean.type = getString(R.string.shutdown)
                mAgendaTypeBean.data.clear()
                exit()
            }
        }
    }

    private fun gotoDataActivity(requestCode: Int) {
        val intent = Intent()
        intent.putExtra(KEY_REQUESTCODE, requestCode)
        intent.putExtra(DATA, mAgendaTypeBean)
        intent.setClass(this, AgendaDataListActivity::class.java)
        gotoActivityForResult(intent, requestCode)
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_agenda_create_work_type
    }

    fun back(view: View?) {
        exit()
    }

    private fun exit() {
        val intent = Intent()
        intent.putExtra(DATA, mAgendaTypeBean)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun onBackPressed() {
        exit()
    }

}
